/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.lzyzsd.circleprogress.demo.slice;

import com.github.lzyzsd.circleprogress.ArcProgress;
import com.github.lzyzsd.circleprogress.CircleProgress;
import com.github.lzyzsd.circleprogress.DonutProgress;
import com.github.lzyzsd.circleprogress.Utils;
import com.github.lzyzsd.circleprogress.utils.properties.model.ArcProgressModel;
import com.github.lzyzsd.circleprogress.utils.properties.model.CircleProgressModel;
import com.github.lzyzsd.circleprogress.utils.properties.model.DountProgressModel;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.DirectionalLayout.LayoutConfig;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;

import java.util.Timer;
import java.util.TimerTask;

public class MainAbilitySlice extends AbilitySlice {
    private DirectionalLayout myLayout = new DirectionalLayout(this);
    private int progress = 0;
    private ArcProgress arcProgressView;
    private CircleProgress circleProgress;
    private DonutProgress donutProgress;
    private boolean isStop = false;
    private Timer timer;

    private TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            progress++;
            if (progress > 100) {
                progress = 0;
            }
            if (isStop) {
                return;
            }
            getUITaskDispatcher().asyncDispatch(() -> {
                arcProgressView.setProgress(progress);
                circleProgress.setProgress(progress);
                donutProgress.setProgress(progress);
            });
        }
    };

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        myLayout.setOrientation(DirectionalLayout.VERTICAL);
        myLayout.setAlignment(LayoutAlignment.HORIZONTAL_CENTER);
        DirectionalLayout.LayoutConfig config = new DirectionalLayout.LayoutConfig(LayoutConfig.MATCH_PARENT, LayoutConfig.MATCH_PARENT);
        myLayout.setLayoutConfig(config);

        int dp20 = Utils.dp2px(getResourceManager(), 20f);
        arcProgressView = new ArcProgress(this);
        LayoutConfig arcProgressLayout = new LayoutConfig(Utils.dp2px(getResourceManager(), 100), Utils.dp2px(getResourceManager(), 100));
        arcProgressLayout.setMargins(0, dp20, 0, 0);

        ArcProgressModel.Builder arcPropertyBuilder = new ArcProgressModel.Builder(getContext(),null);
        arcPropertyBuilder.setTextColor(Color.RED) //文字颜色
                .setFinishedStrokeColor(new Color(0xffffffff)) //进度条已完成部分填充色
                .setUnfinishedStrokeColor(new Color(0xFF486AB0)) //进度条未完成部分填充色
                .setArcAngle(270) //弧角度
                .setBottomText("STORAGE") //底部文字
                .setBottomTextSize(Utils.dp2px(getResourceManager(), 12)) //底部文字大小
                .setTextSize(Utils.dp2px(getResourceManager(), 18)) //中间文字大小
                .setSuffixText("GB") // 中间文字后缀
                .setSuffixTextSize(Utils.dp2px(getResourceManager(), 12)) //后缀文字大小
                .setSuffixTextPadding(Utils.dp2px(getResourceManager(), 10)) //后缀文字和中间文字的间距
                .setStrokeWidth(Utils.dp2px(getResourceManager(), 5)) //进度条宽度
                .setProgress(10) //当前进度
                .setMax(100) //进度最大值
                .build();
        arcProgressView.setStyleWith(arcPropertyBuilder.build());
        arcProgressView.setLayoutConfig(arcProgressLayout);

        circleProgress = new CircleProgress(this);
        CircleProgressModel.Builder circlePropertyBuilder = new CircleProgressModel.Builder(getContext(),null);
        circlePropertyBuilder
                .setFinishedColor(new Color(0xff4291f1)) //进度条已完成部分颜色
                .setUnfinishedColor(new Color(0xffcccccc)) //进度条未完成部分颜色
                .setPrefixText("剩余 ") //前缀文字
                .setSuffixText("% 电量") //后缀文字
                .setTextColor(Color.WHITE) //文字颜色
                .setTextSize(Utils.dp2px(getResourceManager(), 15)) //文字大小
                .setProgress(50) //当前进度
                .setMax(100); //进度最大值
        circleProgress.setStyleWith(circlePropertyBuilder.build());
        LayoutConfig circleProgressLayout = new LayoutConfig(Utils.dp2px(getResourceManager(), 125), Utils.dp2px(getResourceManager(), 125));
        circleProgressLayout.setMargins(0, dp20, 0, 0);
        circleProgress.setLayoutConfig(circleProgressLayout);

        donutProgress = new DonutProgress(this);
        LayoutConfig donutProgressLayout = new LayoutConfig(Utils.dp2px(getResourceManager(), 150), Utils.dp2px(getResourceManager(), 150));
        donutProgressLayout.setMargins(0, dp20, 0, 0);

        DountProgressModel.Builder dountStyleBuilder = new DountProgressModel.Builder(getContext());
        dountStyleBuilder
                .setFinishedStrokeColor(new Color(0xFF4291F1)) //进度条已完成部分颜色
                .setFinishedStrokeWidth(Utils.dp2px(getResourceManager(), 10)) //进度条已完成部分宽度
                .setUnfinishedStrokeColor(new Color(0xffcccccc)) //进度条未完成部分颜色
                .setUnfinishedStrokeWidth(Utils.dp2px(getResourceManager(), 10)) //进度条未完成部分宽度
                .setInnerBackgroundColor(new Color(0xFFFFFFFF)) //内部圈圈颜色
                .setInnerBottomText("LEVEL") //内部下方文字
                .setPrefixText("UP ") //进度前缀
                .setSuffixText("% X") //进度后缀
                .setTextSize(Utils.dp2px(getResourceManager(), 15)) //文字小大
                .setShowText(true) //是否显示文字
                .setStartingDegree(90)
                .setProgress(50) //当前进度
                .setMax(100); //进度最大值

        donutProgress.setStyleWith(dountStyleBuilder.build());
        donutProgress.setLayoutConfig(donutProgressLayout);

        myLayout.addComponent(arcProgressView);
        myLayout.addComponent(circleProgress);
        myLayout.addComponent(donutProgress);
        super.setUIContent(myLayout);

        timer = new Timer("count_timer", false);
        timer.schedule(timerTask, 100, 200);
    }

    @Override
    protected void onStop() {
        isStop = true;
        timerTask.cancel();
        timer.cancel();
        super.onStop();
    }
}
